package com.fzy.silntgym.weight.MSfab.adapters;

/**
 * Created by 77dfeba on 2016/9/10.
 */

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fzy.silntgym.R;
import com.fzy.silntgym.weight.MSfab.models.FavoritesNote;
import com.fzy.silntgym.weight.MSfab.models.FavoritesNote;

/**
 * Created by Gordon Wong on 7/18/2015.
 *
 * Adapter for the all items screen.
 */
public class FavoritesNotesAdapter extends RecyclerView.Adapter<FavoritesNotesAdapter.ViewHolder> {

    private FavoritesNote[] FavoritesNotes;
    private FavoritesNote[] favoritesNotes;

    public FavoritesNotesAdapter(Context context, int numNotes) {
        favoritesNotes = generateNotes(context, numNotes);
    }

    @Override
    public FavoritesNotesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_note, parent,
                false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        FavoritesNote FavoritesNoteModel = favoritesNotes[position];
        String title = FavoritesNoteModel.getTitle();
        String note = FavoritesNoteModel.getNote();
        String info = FavoritesNoteModel.getInfo();
        int infoImage = FavoritesNoteModel.getInfoImage();
        int color = FavoritesNoteModel.getColor();

        // Set text
        holder.titleTextView.setText(title);
        holder.noteTextView.setText(note);
        holder.infoTextView.setText(info);

        // Set image
        if (infoImage != 0) {
            holder.infoImageView.setImageResource(infoImage);
        }

        // Set visibilities
        holder.titleTextView.setVisibility(TextUtils.isEmpty(title) ? View.GONE : View.VISIBLE);
        holder.noteTextView.setVisibility(TextUtils.isEmpty(note) ? View.GONE : View.VISIBLE);
        holder.infoLayout.setVisibility(TextUtils.isEmpty(info) ? View.GONE : View.VISIBLE);

        // Set padding
        int paddingTop = holder.itemView.getContext().
                getResources().getDimensionPixelSize(R.dimen.note_content_spacing);
        holder.noteTextView.setPadding(holder.noteTextView.getPaddingLeft(), paddingTop,
                holder.noteTextView.getPaddingRight(), holder.noteTextView.getPaddingBottom());

        // Set background color
        ((CardView) holder.itemView).setCardBackgroundColor(color);
    }

    @Override
    public int getItemCount() {
        return favoritesNotes.length;
    }

    private FavoritesNote[] generateNotes(Context context, int numNotes) {
        FavoritesNote[] favoritesNotes = new FavoritesNote[numNotes];
        for (int i = 0; i < favoritesNotes.length; i++) {
            favoritesNotes[i] = FavoritesNote.randomNote(context);
        }
        return favoritesNotes;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView titleTextView;
        public TextView noteTextView;
        public LinearLayout infoLayout;
        public TextView infoTextView;
        public ImageView infoImageView;

        public ViewHolder(View itemView) {
            super(itemView);
            titleTextView = (TextView) itemView.findViewById(R.id.note_title);
            noteTextView = (TextView) itemView.findViewById(R.id.note_text);
            infoLayout = (LinearLayout) itemView.findViewById(R.id.note_info_layout);
            infoTextView = (TextView) itemView.findViewById(R.id.note_info);
            infoImageView = (ImageView) itemView.findViewById(R.id.note_info_image);
        }
    }

}